package com.duckhunter.bloodbath;

import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.LayerManager;

public class HunterTwoSprite extends Sprite {

    private static final String IMAGE_URL = "/res/hunter_two_anime.png";
    private static final int FRAME_WIDTH = 34; // 32 / 238 total
    private static final int FRAME_HEIGHT = 64;
    //private static final int H_FRAME_COUNT = 7;
    private static final int FRAME_COUNT = 14;
    private static int[] frame_seq = {0,1,2,3,4,5,6,6,6,6,5,4,3,2};
    //private static final String IMAGE_URL = "/res/hunter_aiming_left.png";
    //private static final int FRAME_WIDTH = 20;
    //private static final int FRAME_HEIGHT = 60;
    int currentFrame;
    // the hunter image
    private Image hunterOneImage;
    // the no of current hunter in display
    private int currentHunterOne;
    // the parent canvas
    private DuckHunterCanvas parent;
    // the parent canvas's layer manager
    private LayerManager manager;
    // the maximum no of hunters to create
    private static final int MAX_HUNTERONE = 1;

    /* Loads HunterOneSprite sprite image and uses the animation constructor method */
    /* HunterOneSprite image file is 32px x 7 frames wide  and 64px tall */
    public HunterTwoSprite(DuckHunterCanvas parent) throws IOException {
        super(Image.createImage(IMAGE_URL), FRAME_WIDTH, FRAME_HEIGHT);
        this.parent = parent;
        this.manager = parent.getManager();
        this.currentFrame = 1;
        this.setFrameSequence(frame_seq);
        setPosition();
    }

    private void setPosition() {
        // generate a random place where hunter appears
        int randomHunterOneX = (parent.getRandom().nextInt(DuckHunterCanvas.GAME_WIDTH));
        int randomHunterOneY =
                (parent.BASE - 77 - parent.getRandom().nextInt(parent.MAX_HEIGHT + 1) -
                getHeight());

        // make sure that these places are within bounds
        if (randomHunterOneX < parent.GAME_ORIGIN_X) {
            randomHunterOneX = parent.CENTER_X;
        }
        if (randomHunterOneY < (parent.BASE - parent.MAX_HEIGHT)) {
            randomHunterOneY = parent.CENTER_Y;
        }
        if (randomHunterOneY > parent.boundryAroundDuckY) {
            randomHunterOneY = parent.CENTER_Y;
        }
        if (randomHunterOneX > parent.boundryAroundDuckX) {
            randomHunterOneX = parent.CENTER_X;
        }
        // set newly created hunter sprite in its random position
        setPosition(randomHunterOneX, randomHunterOneY);

    }

    // creates and displays a hunter at random location
    public void move() {
        if ( currentFrame < FRAME_COUNT ) {
            nextFrame();
            currentFrame++;
        }
        // if maximum hunters are being shown return
        if (currentHunterOne == MAX_HUNTERONE) {
            return;
        }
        setPosition();
        // increase the no of hunters created
        currentHunterOne++;

    }
}
